<template>
    <div>
      <el-form :model="dataForm" :rules="dataRule" v-show="!ftpVisible" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="收集编号" prop="gatherId" v-if="dataForm.gatherId" label-width="120px">
          <el-input v-model="dataForm.gatherId" placeholder="收集编号" disabled></el-input>
      </el-form-item>
      <el-form-item label="收集名称" prop="gatherName" label-width="120px">
          <el-input v-model="dataForm.gatherName" placeholder="收集名称"></el-input>
      </el-form-item>
      <el-form-item label="扫描类型" prop="gatherScanType" label-width="120px">
          <el-radio-group v-model="dataForm.gatherScanType">
                <el-radio  label=0>FTP扫描</el-radio>
                <el-radio  label=1>本地扫描</el-radio>
          </el-radio-group>
      </el-form-item>
      <el-row v-if="dataForm.gatherScanType === '0'">
        <el-col :span="21">
          <el-form-item label="收集ftp" prop="gatherFtpId" label-width="120px">
            <el-select style="width:100%" filterable v-model="dataForm.gatherFtpId" placeholder="收集ftp">
              <el-option v-for=" ftp in ftpList"
              :label="ftp.ftpName"
              :key="ftp.ftpId"
              :value="ftp.ftpId"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="3">
          <el-button icon="el-icon-plus" type="primary" @click="addFtp">新增</el-button>
        </el-col>
      </el-row>
      <el-form-item label="收集根地址" prop="gatherBaseAddress" label-width="120px">
          <el-input v-model="dataForm.gatherBaseAddress" placeholder="收集根地址"></el-input>
      </el-form-item>
      <el-form-item label="存储根地址" prop="localBaseAddress" label-width="120px">
          <el-input v-model="dataForm.localBaseAddress" placeholder="存储根地址"></el-input>
      </el-form-item>
      <el-form-item label="收集参数" prop="gatherParams" label-width="120px">
          <el-input v-model="dataForm.gatherParams" placeholder="收集参数"></el-input>
      </el-form-item>
      </el-form>
      <el-form>
        <ftp-component v-show="ftpVisible" ref="addOrUpdateFtp" @refreshData="refreshData" @disvisible="disvisible"></ftp-component>
      </el-form>
    </div>


</template>

<script>
    import {getObj, addObj, putObj, getAllFtpInfo} from '@/api/gather/configgather'
    import FtpComponent from '../configftp/configftp-component'

    export default {
    data () {
      return {
        visible: false,
        canSubmit: false,
        ftpVisible: false,
        ftpList: [],
        dataForm: {
          gatherId: '',
          gatherName: '',
          gatherFtpId: '',
          gatherBaseAddress: '',
          localBaseAddress: '',
          gatherScanType: '',
          gatherParams: '',
          createBy: '',
          createTime: '',
          updateBy: '',
          updateTime: ''
        },
        dataRule: {
          gatherFtpId: [
            { required: true, message: '收集ftp不能为空', trigger: 'blur' }
          ],
          gatherBaseAddress: [
            { required: true, message: '收集根地址不能为空', trigger: 'blur' }
          ],
          localBaseAddress: [
            { required: true, message: '本地存储根地址不能为空', trigger: 'blur' }
          ],
          gatherParams: [
            { required: true, message: '收集参数不能为空', trigger: 'blur' }
          ],
        }
      }
    },
    components: {
      FtpComponent
    },
    methods: {
      setFtpInfo (ftpInfo) {
        this.$refs.addOrUpdateFtp.initByEntity(ftpInfo)
      },
      addFtp () {
        this.ftpVisible = true
        this.$emit('refreshAddFtpStatus', true)
      },
      init (id) {
        this.visible = true;
        this.canSubmit = true;
        this.ftpVisible = false;
        this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            if (id) {
            getObj(id).then(response => {
                this.dataForm = response.data.data
                console.log(this.dataForm)
            })
          }
          this.refreshFtpInfo()
        })
      },
      async refreshFtpInfo () {
        await getAllFtpInfo().then(response => {
          this.ftpList = response.data.data
        })
      },
      disvisible () {
        this.$emit('refreshDataList')
      },
      checkFtpStatus () {
        this.$refs.addOrUpdateFtp.checkFtpStatus()
      },
      ftpDataFormSubmit () {
        this.$refs.addOrUpdateFtp.dataFormSubmit()
      },
      async refreshData (ftpId) {
        // this.$emit('refreshDataList')
        await this.refreshFtpInfo()
        this.dataForm.gatherFtpId = ftpId
        this.ftpVisible = false
        this.$emit('refreshAddFtpStatus')
      },
      //重置表单
      closeAppendDialog() {
         this.$refs.addOrUpdateFtp.closeDialog()
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.canSubmit = false;
            if (this.dataForm.gatherId) {
                putObj(this.dataForm).then(data => {
                    this.$notify.success('修改成功')
                    this.$emit('refreshConfigGatherDataList', data.data.data.gatherId)
                    this.$emit('configGatherDisvisible')
                }).catch(() => {
                    this.canSubmit = true;
                });
            } else {
                addObj(this.dataForm).then(data => {
                    this.$notify.success('添加成功')
                    this.$emit('refreshConfigGatherDataList', data.data.data.gatherId)
                    this.$emit('configGatherDisvisible')
                }).catch(() => {
                    this.canSubmit = true;
                });
            }
          }
        })
      },
      //重置表单
      closeDialog() {
        this.$refs.addOrUpdateFtp.closeDialog()
        this.$refs["dataForm"].resetFields()
      }
    }
  }
</script>
